/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamananggaranbiayaunitnonrapatkerja;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author Deny Prasetyo
 */
public class DaftarBiayaGroupbox extends Groupbox {

    private Label lblRevisiOrNot;
    private Listbox lstboxDetails;
    private Label lblNamaPosBiayaUpper;
    private Label lblNamaPosBiayaLower;
    private Label lblTotalAnggaran;
    private DecimalFormat df = new DecimalFormat("#,##0.0");
    private boolean kunci = false;
    private PerekamanAnggaranBiayaUnitNonRapatKerjaDAO perekamanAnggaranBiayaUnitNonRapatKerjaDAO = new PerekamanAnggaranBiayaUnitNonRapatKerjaDAOImpl();
    private PerekamanAnggaranBiayaUnitNonRapatKerjaWnd froms;

    public PerekamanAnggaranBiayaUnitNonRapatKerjaWnd getFroms() {
        return froms;
    }

    public void setFroms(PerekamanAnggaranBiayaUnitNonRapatKerjaWnd froms) {
        this.froms = froms;
    }

    public boolean isKunci() {
        return kunci;
    }

    public void setKunci(boolean kunci) {
        this.kunci = kunci;
    }

    public void initializeChildren() {
        String id = this.getId();
        lblRevisiOrNot = (Label) getFellow("lblRevisiOrNot" + id);
        lstboxDetails = (Listbox) getFellow("lstboxDetails" + id);
        lblNamaPosBiayaUpper = (Label) getFellow("lblNamaPosBiayaUpper" + id);
        lblNamaPosBiayaLower = (Label) getFellow("lblNamaPosBiayaLower" + id);
        lblTotalAnggaran = (Label) getFellow("lblTotalAnggaran" + id);
    }

    public Label getLblNamaPosBiayaLower() {
        return lblNamaPosBiayaLower;
    }

    public Label getLblNamaPosBiayaUpper() {
        return lblNamaPosBiayaUpper;
    }

    public Label getLblRevisiOrNot() {
        return lblRevisiOrNot;
    }

    public Label getLblTotalAnggaran() {
        return lblTotalAnggaran;
    }

    public Listbox getLstboxDetails() {
        return lstboxDetails;
    }

    public void setNamaPosBiaya(String namaPosBiaya) {
        this.lblNamaPosBiayaLower.setValue(namaPosBiaya);
        this.lblNamaPosBiayaUpper.setValue(namaPosBiaya);
    }

    public void setTotalAnggaranBiaya(Double total) {
        this.lblTotalAnggaran.setValue(df.format(total));
    }

    public void setRevisiOrNot(String revisi) {
        this.lblRevisiOrNot.setValue(revisi);
    }

    public void setListboxItems(List<Map> items) {
        //Parameters item,kuantitas,satuan,harga,jumlah

        double total = 0.0;
        for (final Map m : items) {
            Listitem item = new Listitem();
            item.setValue(m);
            item.appendChild(new Listcell(ClassAntiNull.AntiNullString(m.get("item"))));
            item.appendChild(new Listcell(ClassAntiNull.AntiNullIntString(m.get("kuantitas"))));
            item.appendChild(new Listcell(ClassAntiNull.AntiNullString(m.get("satuan"))));
            item.appendChild(new Listcell(ClassAntiNull.AntiNullDoubleCurrency(m.get("harga"))));
            total += ClassAntiNull.AntiNullDouble(m.get("jumlah"));
            item.appendChild(new Listcell(ClassAntiNull.AntiNullDoubleCurrency(m.get("jumlah"))));

            final Toolbarbutton toolbarHapus = new Toolbarbutton();
            toolbarHapus.setLabel("[hapus]");

            toolbarHapus.addEventListener("onClick", new EventListener() {

                public void onEvent(Event arg0) throws Exception {
                    if (!isKunci()) {
                        if (Messagebox.show("Yakin Akan dihapus", "Pesan Konfirmasi", Messagebox.OK | Messagebox.CANCEL, null) == Messagebox.OK) {
                            perekamanAnggaranBiayaUnitNonRapatKerjaDAO.delete(ClassAntiNull.AntiNullString(m.get("tahun")), ClassAntiNull.AntiNullString(m.get("kodeUnit")), ClassAntiNull.AntiNullInt(m.get("kodePosBiaya")), ClassAntiNull.AntiNullInt(m.get("kodeItemBiayaBarang")), ClassAntiNull.AntiNullString(m.get("isRevisi")));
                            froms.load();
                            froms.showDetails();
                        }
                    } else {
                        Messagebox.show("Dikunci", "Pesan Kesalahan", Messagebox.OK | Messagebox.CANCEL, null);
                    }
                }
            });

            if (isKunci()) {
                toolbarHapus.setDisabled(true);
            } else {
                toolbarHapus.setDisabled(false);
            }
            Listcell buttons = new Listcell();
            buttons.appendChild(toolbarHapus);
            item.appendChild(buttons);

            lstboxDetails.appendChild(item);
        }
        setTotalAnggaranBiaya(total);
    }
}
